<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerm('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="PermList" row-key="id">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button type="text" @click="addPerm(row.id)">添加</el-button>
            <el-button type="text" @click="editPerm(row.id)">编辑</el-button>
            <el-button type="text" @click="Removepermissions(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${showText}权限点`" :visible="isShowDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { Getallpermission, delPerm, getPermDetail, Theeditor, addPerm } from '../../api/permission'
import { listToTrData } from '../../utils/index'
export default {
  data() {
    return {
      PermList: [],
      formData: {
        code: '',
        description: '',
        enVisible: '1',
        name: '',
        pid: '',
        type: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      isShowDialog: false
    }
  },
  // 计算属性
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    // 删除权限
    async Removepermissions(id) {
      // 二次确认
      await this.$confirm('确认删除?')
      await delPerm(id)
      this.$message.success('删除成功')
      // 刷新数据
      this.getPermList()
    },
    // 请求数据渲染
    async  getPermList() {
      this.PermList = listToTrData(await Getallpermission(), '0')
      console.log(37, '所有权限点', this.PermList)
    },
    // 添加权限
    addPerm(pid, type) {
      this.formData.pid = pid
      this.formData.type = type

      this.isShowDialog = true
    },

    // 编辑权限
    async editPerm(id) {
      // 回显数据
      this.formData = await getPermDetail(id)
      this.isShowDialog = true
    },

    // 确定
    async  btnOK() {
      // 校验表单
      await this.$refs.perForm.validate()
      // 判断,formData有id就是编辑
      if (this.formData.id) {
        // 编辑
        // 发起编辑请求
      // 把我们编辑好的整个数据，也就是 formData 传给后端
        await Theeditor(this.formData)
      } else {
        // 添加
        // 把我们添加好的数据，也就是 formData整个对象，传出去
        await addPerm(this.formData)
      }

      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 清空数据
      this.formData = {}
      // 刷新页面
      this.getPermList()
    },
    // 取消
    btnCancel() {}
  }
}
</script>

<style>

</style>
